#include "../func.h"

void cleanup1(void* p)
{
    printf("I am cleanup func\n");
    free(p);
}

void cleanup2(void* p)
{
    printf("I am cleanup func2\n");
}

void* threadfunc(void* p)
{
    char* p1;
    printf("I am child thread\n");
    p1 = (char*)malloc(20);
    pthread_cleanup_push(cleanup1, p1);
    sleep(5);
    pthread_exit((void*)5);
    pthread_cleanup_pop(1);
}

int main()
{
    pthread_t pthid;
    int ret;
    ret = pthread_create(&pthid, NULL, threadfunc, NULL);
    
    sleep(1);
    ret = pthread_cancel(pthid);

    long l = 0;
    pthread_join(pthid, (void**)&l);
    printf("I am main thread, %d\n", l);

    return 0;
}
